package br.univas.bean;


import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import br.univas.dao.UserDAO;
import br.univas.to.UserTO;

@ManagedBean(name="userBean")
@RequestScoped
public class UserBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private UserTO userTo;
	private UserDAO userDao;
	
	public UserBean() {
		userTo = new UserTO();
		userDao = new UserDAO();
	}

	
	
	public void login(){
		System.out.println(this.userTo.getEmail());
		System.out.println(this.userTo.getPassword());
		if(this.userTo.getEmail().equals("Admin") && this.userTo.getPassword().equals("Admin")){
			try {
				System.out.println("entra no try");
				FacesContext.getCurrentInstance().getExternalContext().redirect("admin/gerenciamento.xhtml");
			} catch (Exception e) {
				System.out.println("entra no cath");
			}
		}else {
			if(this.userDao.veryfyUserLogin(userTo)){
				try {
					System.out.println("entra no try");
					FacesContext.getCurrentInstance().getExternalContext().redirect("faces/index.xhtml");
				} catch (Exception e) {
					System.out.println("entra no cath");
				}
			}else {
				
			}
		}
	}
	
	
	/*CRUD*/
	public void saveUser(){
		if(this.userDao.saveUserGraph(this.userTo)){
						System.out.println("saveUser ok");
		} else{
			System.out.println("Erro ao inserir o usuario");
		}
		
		
	}
	/*Responsavel pela travessia Dao and To*/
	public UserTO getUserTo() {
		return userTo;
	}

	public void setUserTo(UserTO userTo) {
		this.userTo = userTo;
	}

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}
	
	

}